package com.md.application.service;

import com.md.api.OpenSystemApi;
import com.md.application.assembler.ApiAssembler;
import com.md.application.assembler.ModuleAssembler;
import com.md.base.exception.ServerException;
import com.md.domain.model.po.ApiPO;
import com.md.domain.model.po.AppInfoPO;
import com.md.domain.service.AppDomainService;
import com.md.infrastructure.value.ErrorEnum;
import com.md.model.system.CommonResult;
import com.md.request.ApiRequest;
import com.md.request.AppInfoRequest;
import com.md.request.BatchIdRequest;
import com.md.request.QueryAppInfoRequest;
import com.md.request.QueryModuleRequest;
import com.md.response.ApiResponse;
import com.md.response.AppInfoResponse;
import com.md.response.ModuleResponse;
import com.md.util.ObjectUtil;
import com.md.value.constants.DefaultServerContext;
import com.md.value.server.ServerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.md.application.assembler.AppInfoAssembler.buildAppInfoRequest2PO;
import static com.md.application.assembler.AppInfoAssembler.buildQueryAppInfoBO;

/**
 * @author 刘骄阳
 * 2022-09-24 1:35
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OpenSystemApplicationService implements OpenSystemApi {
  private final AppDomainService appDomainService;

  @Override
  public CommonResult<AppInfoResponse> openQuery(final QueryAppInfoRequest request) {
    return CommonResult.success(appDomainService.query(buildQueryAppInfoBO(request)));
  }

  @Override
  public CommonResult<List<AppInfoResponse>> openQueryList(final QueryAppInfoRequest request) {
    return CommonResult.success(appDomainService.queryAppInfo(buildQueryAppInfoBO(request)));
  }

  @Override
  public CommonResult<List<AppInfoResponse>> openQueryIdsList(final BatchIdRequest request) {
    return CommonResult.success(appDomainService.queryAppInfo(request.getIds()));
  }

  @Override
  public CommonResult<Void> openCreateAppInfo(final AppInfoRequest request) {
    AppInfoPO appInfoPO = buildAppInfoRequest2PO(request);
    appDomainService.createAppInfo(appInfoPO, request.getModuleId());
    return CommonResult.success();
  }

  @Override
  public CommonResult<Void> openCreateApi(final ApiRequest request) {
    ApiAssembler.checkParam(request);
    ApiPO apiPO = ApiAssembler.buildApiRequest2PO(request);
    Long appInfoId = request.getAppInfoId();
    AppInfoResponse appInfoResponse = appDomainService.queryAppInfo(appInfoId);
    ServerContext.put(DefaultServerContext.TENANT_ID, appInfoResponse.getTenantId());
    ServerContext.put(DefaultServerContext.BRAND_ID, appInfoResponse.getBrandId());
    appDomainService.createApi(apiPO);
    return CommonResult.success();
  }

  @Override
  public CommonResult<List<ModuleResponse>> openQueryList(final QueryModuleRequest request) {
    return CommonResult.success(appDomainService.queryModuleList(ModuleAssembler.buildQueryModuleBO(request)));
  }

  @Override
  public CommonResult<List<ApiResponse>> openQueryList(final ApiRequest request) {
    return CommonResult.success(appDomainService.queryApiList(ApiAssembler.buildQueryApiBO(request)));
  }

  @Override
  public CommonResult<ApiResponse> openQuery(final ApiRequest request) {
    return CommonResult.success(appDomainService.queryApi(ApiAssembler.buildQueryApiBO(request)));
  }

  @Override
  @Cacheable(value = "QUERY_SYSTEM_API", key = "#request.method+'_'+#request.url")
  public CommonResult<ApiResponse> openQueryByUrlMethod(final ApiRequest request) {
    if (ObjectUtil.hasEmpty(request.getMethod(), request.getUrl())) {
      throw ServerException.build(ErrorEnum.PARAM_IS_ALL_EMPTY);
    }
    return CommonResult.success(appDomainService.queryApi(ApiAssembler.buildQueryApiBO(request)));
  }

  @Override
  public CommonResult<List<ModuleResponse>> openQueryListByIdList(final QueryModuleRequest request) {
    return CommonResult.success(appDomainService.queryModuleListByIdList(request.getIdList()));
  }

  @Override
  public CommonResult<List<AppInfoResponse>> openQueryListByModuleId(final QueryAppInfoRequest request) {
    return CommonResult.success(appDomainService.queryListByModuleId(request.getModuleId()));
  }
}
